<template>
  <div v-show="isShow">
    <el-tree default-expand-all :data="treeData" :props="{label:'name'}" @node-click="selectNode" />
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments.js'
import { tranListToTreeData } from '@/utils/index.js'

export default {
  name: 'Tree',
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: []
    }
  },
  created () {
    this.getDepartmentsFN()
  },
  methods: {
    // 点击内容树形结构，获取点击的内容，关闭树形结构
    selectNode(node) {
      // console.log(node)
      this.$emit('updateDept', node.name)
      this.$emit('update:isShow', false)
    },
    // 获取树形结构的内容
    async getDepartmentsFN() {
      const res = await getDepartments()
      // 机构格式化
      this.treeData = tranListToTreeData(res.depts)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
